package res

import (
	"encoding/json"
	"fmt"
)

type Res []byte
type ResString string
type Result *ResJson

/**
 *	返回值对象
**/
type ResJson struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

func New(code int, msg string, data interface{}) *ResJson {
	return &ResJson{
		Code: code,
		Msg:  msg,
		Data: data,
	}
}

func (rj *ResJson) ToJson() map[string]any {
	m := make(map[string]any)
	m["code"] = rj.Code
	m["msg"] = rj.Msg
	m["data"] = rj.Data
	return m
}

func (rj *ResJson) JsonToBytes() Res {
	res, err := json.Marshal(*rj)
	if err != nil {
		fmt.Println("resjson to json failed!", err)
		return nil
	}
	return res
}
